home *** CD-ROM | disk | FTP | other *** search
/ Reverse Code Engineering RCE CD +sandman 2000 / ReverseCodeEngineeringRceCdsandman2000.iso / RCE / Ebooks / Thinking in C++ V2 / C17 / ichar_traits.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-25  |  1.3 KB  |  49 lines

  1. //: C17:ichar_traits.h
  2. // From Thinking in C++, 2nd Edition
  3. // Available at http://www.BruceEckel.com
  4. // (c) Bruce Eckel 1999
  5. // Copyright notice in Copyright.txt
  6. // Creating your own character traits
  7. #ifndef ICHAR_TRAITS_H
  8. #define ICHAR_TRAITS_H
  9. #include <string>
  10. #include <cctype>
  11.  
  12. struct ichar_traits : std::char_traits<char> {
  13.   // We'll only change character by 
  14.   // character comparison functions
  15.   static bool eq(char c1st, char c2nd) {
  16.     return 
  17.       std::toupper(c1st) == std::toupper(c2nd);
  18.   }
  19.   static bool ne(char c1st, char c2nd) {
  20.     return 
  21.       std::toupper(c1st) != std::toupper(c2nd);
  22.   }
  23.   static bool lt(char c1st, char c2nd) {
  24.     return 
  25.       std::toupper(c1st) < std::toupper(c2nd);
  26.   }
  27.   static int compare(const char* str1, 
  28.     const char* str2, size_t n) {
  29.     for(int i = 0; i < n; i++) {
  30.       if(std::tolower(*str1)>std::tolower(*str2))
  31.         return 1;
  32.       if(std::tolower(*str1)<std::tolower(*str2))
  33.         return -1;
  34.       if(*str1 == 0 || *str2 == 0)
  35.         return 0;
  36.       str1++; str2++; // Compare the other chars
  37.     }
  38.     return 0;
  39.   }
  40.   static const char* find(const char* s1, 
  41.     int  n, char c) {
  42.     while(n-- > 0 &&  
  43.       std::toupper(*s1) != std::toupper(c))
  44.       s1++;
  45.     return s1;
  46.   }
  47. };
  48. #endif // ICHAR_TRAITS_H  ///:~
  49.